package com.meilingcloud.biobank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.SampleType;
import com.meilingcloud.biobank.mapper.SampleTypeMapper;
import com.meilingcloud.biobank.service.ISampleTypeService;
import com.meilingcloud.common.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 样本类型 Service业务层处理
 *
 * @author chenl
 * @date 2021-03-04
 */
@Service
public class SampleTypeServiceImpl extends ServiceImpl<SampleTypeMapper, SampleType> implements ISampleTypeService {

    @Override
    public List<SampleType> queryList(SampleType sampleType) {
        LambdaQueryWrapper<SampleType> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sampleType.getSampTypeCode())) {
            lqw.eq(SampleType::getSampTypeCode, sampleType.getSampTypeCode());
        }
        if (StringUtils.isNotBlank(sampleType.getSampTypeName())) {
            lqw.like(SampleType::getSampTypeName, sampleType.getSampTypeName());
        }
        if (StringUtils.isNotBlank(sampleType.getSampTypeDesc())) {
            lqw.eq(SampleType::getSampTypeDesc, sampleType.getSampTypeDesc());
        }
        return this.list(lqw);
    }

    @Cacheable(value = Constants.SAMPLE_TYPE_KEY, key = "#sampTypeId", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public SampleType queryById(Long sampTypeId) {
        return this.getById(sampTypeId);
    }

    @Cacheable(value = Constants.SAMPLE_TYPE_KEY, key = "#code", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public SampleType queryByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<SampleType>().eq(SampleType::getSampTypeCode, code).last("limit 1"));
    }

    @CacheEvict(value = Constants.SAMPLE_TYPE_KEY, allEntries = true)
    @Override
    public boolean updateSampTypeById(SampleType sampleType) {
        return this.updateById(sampleType);
    }

    @CacheEvict(value = Constants.SAMPLE_TYPE_KEY, allEntries = true)
    @Override
    public boolean removeSampTypeByIds(List<Long> sampTypeIds) {
        return this.removeByIds(sampTypeIds);
    }


}
